Skip to content

Fix GH-9589: dl() segfaults when module is already loaded - #9689

Closed
cmb69 wants to merge 2 commits into
php:PHP-8.2from
cmb69:cmb/gh9589
Closed

Fix GH-9589: dl() segfaults when module is already loaded#9689
cmb69 wants to merge 2 commits into
php:PHP-8.2from
cmb69:cmb/gh9589

Conversation

@cmb69

@cmb69 cmb69 commented Oct 7, 2022

Copy link
Copy Markdown
Member

As of PHP 8.2.0, zend_module_entry structures are no longer copied, so when a module is permanently loaded, and users try to dynamically load that module again, the structure is corrupted[1], causing a segfault on shutdown.

We catch that by checking whether any dynamically loaded module is already loaded, and bailing out in that case without modifying the zend_module_entry structure.

[1] #9589 (comment)

As of PHP 8.2.0, `zend_module_entry` structures are no longer copied,
so when a module is permanently loaded, and users try to dynamically
load that module again, the structure is corrupted[1], causing a
segfault on shutdown.

We catch that by checking whether any dynamically loaded module is
already loaded, and bailing out in that case without modifying the
`zend_module_entry` structure.

[1] <php#9589 (comment)>
@cmb69

cmb69 commented Oct 7, 2022

Copy link
Copy Markdown
Member Author

Note that this may be applied with or without also applying PR #9648; while that duplicates a bit of code in the former case (where it is not strictly necessary), it still seems to be good practice to catch this condition early, and to fail fast.

/cc @arnaud-lb, @morrisonlevi

--TEST--
dl() segfaults when module is already loaded
--EXTENSIONS--
dl_test

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It's probably sensible to add a comment regarding the usage of dl_test; actually, it doesn't matter which extension we use for the test, but it is mandatory that it is a shared module; this is always given for dl_test.

Comment thread ext/standard/dl.c

@arnaud-lb arnaud-lb left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good to me

@iluuu1994 iluuu1994 left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

Comment thread ext/standard/dl.c
@cmb69 cmb69 closed this in 6e0505b Oct 10, 2022
@cmb69
cmb69 deleted the cmb/gh9589 branch October 10, 2022 11:39
adrian-enspired pushed a commit to adrian-enspired/php-src that referenced this pull request Aug 4, 2026
As of PHP 8.2.0, `zend_module_entry` structures are no longer copied,
so when a module is permanently loaded, and users try to dynamically
load that module again, the structure is corrupted[1], causing a
segfault on shutdown.

We catch that by checking whether any dynamically loaded module is
already loaded, and bailing out in that case without modifying the
`zend_module_entry` structure.

[1] <php#9589 (comment)>

Closes phpGH-9689.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants